<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ChatGPT UI</title>
    <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
    <style>
        html,body{
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #F2F3F5;
        }
        .chatBox{
            padding: 10px;
            width: 900px;
            height: 500px;
            border-bottom: 1px solid #ddd;
            background-color: #fff;
            overflow-y: auto;
        }
        .sendBox{
            display: flex;
            flex-direction: column;
            align-items: end;
        }
        textarea{
            padding: 10px;
            width: 900px;
            resize: none;
            border: none;
            outline: none;
        }
        .avatar img{
            border-radius: 50%;
            margin-right: 10px;
        }
        .chatItem{
            display: flex;
            /* align-items: center; */
            margin: 10px 0;
        }
        .message,.myself{
            font-size: 14px;
            background-color: #F2F3F5;
            padding: 5px 10px 0 10px;
            margin-right: 10px;
            border-radius: 4px;

        }
        p{
            margin: 5px 0;
        }
        pre{
            width: calc(100% - 10px);
            display: block;
            background-color: #333;
            color: #fff;
            padding: 5px;
        }
        button{
            width: 100px;
            margin-top: 10px;
            background-color: #027BFF;
            border: none;
            color: #fff;
            height: 35px;
            cursor: pointer;
            border-radius: 4px;
        }
        button:hover{
            opacity: 0.8;
        }
        button:active{
            opacity: 1;
        }
        .myself{
            background-color: #ECF5FF;
            color: #000;
        }
        h2{
            font-weight: 400;
            font-family: 'Times New Roman', Times, serif;
            color: #777;
        }
    
    </style>
</head>
<body>
    <header><h2>Huhailong ChatGPT Window</h2></header>
    <main>
        <div class="chatBox" id="chatBox">
            <div class="chatItem">
                <div class="avatar">
                    <img src="./icon/gpt.png" width="35px" height="35px"/>
                </div>
                <div class="message">
                    <p>This is chat message. </p>
                </div>
            </div>
            <div class="chatItem">
                <div class="avatar">
                    <img src="./icon/gpt.png" width="35px" height="35px"/>
                </div>
                <div class="message">
                    <p>This is chat message. I am AI generate message. My name is <b>Huhailong</b> and I from China. I like run and code. <br/> 
                        <pre id="code-1"></pre> 
                    </p>
                </div>
            </div>
            <div class="chatItem">
                <div class="avatar">
                    <img src="./icon/gpt.png" width="35px" height="35px"/>
                </div>
                <div class="message">
                    <p>中文消息效果展示，本页面使用纯html、css、js编写。 </p>
                </div>
            </div>
        </div>
        <div class="sendBox">
            <textarea placeholder="输入问题" id="chat" rows="6"></textarea>
            <button onclick="submitChat()">发送</button>
        </div>
    </main>
    <script>
        const javaCode = 'public class Test {\n\n    public static void main(String[]args){\n\n        System.out.println("Hello World");\n\n    }\n}\n';
        const code = document.getElementById("code-1");
        code.innerText=javaCode;
        const wsUrl = 'ws://127.0.0.1:8080'
        const textarea = document.getElementById("chat");
        let newFlag = true
        let msgId = 0;
        textarea.addEventListener('keydown',(e)=>{
            if(e.code === 'Enter'){
                submitChat()
            }
            
        })

        socket = new WebSocket(wsUrl)

        let connectFlag = false;

        socket.onopen = function(e) {
            connectFlag = true;
        }

        socket.onmessage = function(e) {
            let chatBox = document.getElementById("chatBox");
            if(newFlag){
                msgId = new Date().getTime();
                chatBox.appendChild(buildMessageItem(e.data,false,'msg-'+msgId));
                newFlag = false;
            }else{
                let textNode = document.createTextNode(e.data);
                document.getElementById('msg-'+msgId).appendChild(textNode);
            }
            chatBox.scrollTo(0,chatBox.scrollHeight);
        }
        socket.onclose = function(e) {
            let chatBox = document.getElementById("chatBox");
            chatBox.appendChild(buildMessageItem('<span style="color:red;">与服务器的连接已断开</span>',false));
            chatBox.scrollTo(0,chatBox.scrollHeight);
            connectFlag = false;
        }
        socket.onerror = function(e) {
            let chatBox = document.getElementById("chatBox");
            chatBox.appendChild(buildMessageItem('<span style="color:red;">'+e+'</span>',false));
            chatBox.scrollTo(0,chatBox.scrollHeight);
        }

        function sendMessage(message) {
            socket.send(message)
        }
        
        function submitChat() {
            let chatMessage = textarea.value;
            let chatBox = document.getElementById("chatBox");
            chatBox.appendChild(buildMessageItem(chatMessage,true,'slef-'+new Date().getTime()));
            textarea.value='';
            chatBox.scrollTo(0,chatBox.scrollHeight);
            sendMessage(chatMessage)
            sendSounds();
            newFlag = true;
        } 

        function buildMessageItem(text,isSelf,msgId) {
            let chatItem = document.createElement("div");
            let avatarDiv = document.createElement("div");
            let avatarImg = document.createElement("img");
            let message = document.createElement("div");
            let messageText = document.createElement("p");
            chatItem.className = "chatItem";
            avatarDiv.className = "avatar";
            avatarImg.width = 35;
            avatarImg.height = 35;
            if(isSelf){
                avatarImg.src = "./icon/huhailong.png";
                message.className = "myself";
            }else{
                avatarImg.src = "./icon/gpt.png";
                message.className = "message";
                message.id = msgId
            }
            //组装
            chatItem.appendChild(avatarDiv);
            chatItem.appendChild(message);
            avatarDiv.appendChild(avatarImg);
            message.append(messageText);
            messageText.appendChild(document.createTextNode(text))
            return chatItem;
        }

        function sendSounds(){
            let sendMp3 = new Audio('./mp3/send.mp3')
            sendMp3.play()
        }
        
    </script>
</body>
</html>